﻿using System;
using curbside.IBatis.Common;
using System.Configuration;
using curbside.SqlHelper;

namespace curbside.IBatis
{
    /// <summary>
    /// 数据库操作帮助类
    /// </summary>
    public class DAOHelper
    {
        #region ibatis

        /// <summary>
        /// 获取指定数据库会话的ICommonDAO
        /// </summary>
        /// <param name="context">数据库会话枚举</param>
        /// <returns></returns>
        public static ICommonDAO CreateCommonDAO(DBContext context)
        {
            return CreateCommonDAO(context.ToString());
        }

        /// <summary>
        /// 获取指定数据库会话的ICommonDAO
        /// </summary>
        /// <param name="contextName">数据库会话名称</param>
        /// <returns></returns>
        public static ICommonDAO CreateCommonDAO(string contextName)
        {
            return DaoFactroy.GetContextDAO<ICommonDAO>(contextName);
        }

        /// <summary>
        /// 获取默认数据库会话的ICommonDAO
        /// </summary>
        /// <returns></returns>
        public static ICommonDAO CreateCommonDAO()
        {
            return CreateCommonDAO(DBContext.Context1.ToString());
        }


        #endregion

        #region sqlhelper

        /// <summary>
        /// 获取默认SqlHelper
        /// </summary>
        public static ISqlHelper DefaultSqlHelper
        {
            get
            {
                return GetSqlHelper();
            }
        }

        /// <summary>
        /// 获取默认SqlHelper
        /// </summary>
        /// <returns></returns>
        public static ISqlHelper GetSqlHelper()
        {
            return GetSqlHelper(DBContext.Context1);
        }

        /// <summary>
        /// 获取指定数据库会话的SqlHelper
        /// </summary>
        /// <param name="context">数据库会话枚举</param>
        /// <returns></returns>
        public static ISqlHelper GetSqlHelper(DBContext context)
        {
            return GetSqlHelper(context.ToString());
        }

        /// <summary>
        /// 获取指定数据库会话的SqlHelper
        /// </summary>
        /// <param name="contextName">数据库会话名称</param>
        /// <returns></returns>
        public static ISqlHelper GetSqlHelper(string contextName)
        {
            string connectionString = string.Empty;
            ConnectionStringSettings connectingStringSettings = ConfigurationManager.ConnectionStrings[contextName];
            if (connectingStringSettings == null)
            {
                throw new ArgumentException(string.Format("找不到名称为 {0} 的链接字符串", contextName));
            }
            connectionString = connectingStringSettings.ConnectionString;
            return DataLayerHelper.CreateSqlHelper(connectionString);
        }

        /// <summary>
        /// 获取指定数据库连接字符串的SqlHelper
        /// </summary>
        /// <param name="connectionString">数据库连接字符串</param>
        /// <returns></returns>
        public static ISqlHelper GetSqlHelper2(string connectionString)
        {
            return DataLayerHelper.CreateSqlHelper(connectionString);
        }

        #endregion
    }

    /// <summary>
    /// 数据库会话标识
    /// 该枚举值需要与connectionStrings节点的name值相同 或
    /// 与dao.config文件中的context节点id属性相同
    /// </summary>
    public enum DBContext
    {
        Context1,
        Context2,
        Context3,
        Context4,
        Context5
    }
}
